package com.example.service.webhook.business.court.service;

import com.example.service.webhook.business.court.api.ZhixingCourtClient;
import com.example.service.webhook.business.court.billitem.ZhixingCourtBillTask;
import com.example.service.webhook.business.court.dto.ZhixingCourtCaseDetail;
import com.example.service.webhook.business.court.entity.ZhixingCourtCaseDetailEntity;
import com.example.service.webhook.business.court.repository.ZhixingCourtCaseDetailRepository;
import com.example.service.webhook.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by zouwanli on 17/5/31.
 */
@Service
public class ZhixingCourtService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ZhixingCourtService.class);

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private ZhixingCourtCaseDetailRepository zhixingCourtCaseDetailRepository;

    @Autowired
    private ZhixingCourtClient zhixingCourtClient;

    public void fetchBill(final ZhixingCourtBillTask task){

        taskExecutor.execute(new Runnable() {

            @Override
            public void run() {

                try {


                    Date startTime = DateUtil.getCurrentDate();

                    int count = task.getCount();

                    if (count > 0) {

                        List<ZhixingCourtCaseDetail> zhixingCourtCaseDetails = zhixingCourtClient.getZhixingCourtCaseDetail(task.getTaskId());

                        for (ZhixingCourtCaseDetail zhixingCourtCaseDetail: zhixingCourtCaseDetails) {

                            ZhixingCourtCaseDetailEntity  zhixingCourtCaseDetailEntity = new ZhixingCourtCaseDetailEntity();
                            BeanUtils.copyProperties(zhixingCourtCaseDetail, zhixingCourtCaseDetailEntity);
                            zhixingCourtCaseDetailEntity.setUserId(task.getUserId());

                            ZhixingCourtCaseDetailEntity oldZhixingCourtCaseDetailEntity
                                    = zhixingCourtCaseDetailRepository.getZhixingCourtCaseDetail(task.getUserId(),
                                    zhixingCourtCaseDetail.getMappingId(), zhixingCourtCaseDetail.getCaseId());

                            if (oldZhixingCourtCaseDetailEntity != null){
                                zhixingCourtCaseDetailEntity.setId(oldZhixingCourtCaseDetailEntity.getId());
                                zhixingCourtCaseDetailEntity.setCreateTime(oldZhixingCourtCaseDetailEntity.getCreateTime());
                                zhixingCourtCaseDetailRepository.updateZhixingCourtCaseDetail(zhixingCourtCaseDetailEntity);
                            }else {

                                zhixingCourtCaseDetailRepository.saveZhixingCourtCaseDetail(zhixingCourtCaseDetailEntity);
                            }
                        }

                    }else {

                        LOGGER.info("There were no cases for task, taskId: {}", task.getTaskId());
                    }


                }catch (Exception ex){

                    LOGGER.error("ZhixingCourtCaseDetail callback error,taskId:{}, exception: {}", task.getTaskId(), ex);
                }

            }
        }
        );
    }
}
